# -*- coding: utf-8 -*-

import glob
import string
import re
import d15n_routines as r
from salt_file import salt

n = '\x0D\x0A'

if 1: # calculate sha256 digests
    
    for f in glob.glob('_cleanup_POC_ID_firstname_surname_v3.csv'):

        i = open(f, 'r')
        o = open('_d15n_final' + f, 'wb')
        o.write('ID\tMA_Firstname_d15n\tMA_Surname_d15n\tMA_Surname_IFWOMAN_d15n' + n)

        for l in i.readlines():
        
            l = r.remove_eol(l)
        
            id, nm, sm, mw, mm = string.split(l, '\t')

            if nm <> r'<NULL>': 
                dig_nm = r.sha256(nm + salt)
            else:
                dig_nm = ''
            if sm <> r'<NULL>':
                dig_sm = r.sha256(sm + salt)
            else:
                dig_sm = ''
            
            if mw == 'w': dig_mm = r.sha256(mm + salt)
            else: dig_mm = ''

            #test run
            #res = "%s\t%s\t%s\t%s\t%s" % (l, id, dig_nm, dig_sm, dig_mm) + n
            #final run ... original line (l) ommited
            res = "%s\t%s\t%s\t%s" % (id, dig_nm, dig_sm, dig_mm) + n
            o.write(res)

        o.close()
